package com.brkj.util.view;

import java.util.List;

import net.tsz.afinal.FinalBitmap;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.brkj.model.DS_Course;
import com.brkj.model.DS_CourseClass;
import com.brkj.main.R;
import com.brkj.util.ImgShow;
import com.nostra13.universalimageloader.core.ImageLoader;

public class BaseActionBarSelectAdapter extends BaseAdapter{
	private ListItemView listItemView;
	protected LayoutInflater listContainer; 
	private Context context;
	private List<BaseActionBarItem> itemList;
	private PopupWindow popupWindow;
	private TextView title;
	
	public BaseActionBarSelectAdapter(Context context, List<BaseActionBarItem> itemList, PopupWindow popupWindow, TextView title){
		this.context = context;
		this.itemList = itemList;
		this.popupWindow = popupWindow;
        if(title != null){
            this.title = title;
        }

		
		listContainer = LayoutInflater.from(context);
	}
	
	private static class ListItemView{
		public TextView itemName;
	};
	
	@Override
	public int getCount() {
		return itemList.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 自定义视图
		listItemView = new ListItemView();
		// 获取list_item布局文件的视图
		if (convertView == null) {
			convertView = listContainer.inflate(R.layout.pop_item, null);
			// 获取控件对象
			listItemView.itemName = (TextView) convertView.findViewById(R.id.courseClassName);

			// 设置控件集到convertView
			convertView.setTag(listItemView);
		} else {
			listItemView = (ListItemView) convertView.getTag();
		}
		
		listItemView.itemName.setText(itemList.get(position).getItemName());
		
		listItemView.itemName.setTag(position);
		listItemView.itemName.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				int pos = (Integer)v.getTag();
				popupWindow.dismiss();

                if(title!= null){
                    title.setText(itemList.get(pos).getItemName());
                }

				itemList.get(pos).getClickListener().onClick();
			}
		});
		
		return convertView;
	}
	
}
